﻿using System.Collections.Generic;
using System.Data;
using System.Linq;
using System;

namespace SlamCms.Data.Repositories
{
	public class CommentRepository
	{
		private readonly IDbConnection connection;

		public CommentRepository(IDbConnection connection)
		{
			this.connection = connection;
		}

        public IEnumerable<CommentResource> GetComments(Guid SiteId)
        {
            string query = @"   select C.CommentID, C.UserId, C.CreatedDate, C.PrimaryContentItemId as ContentItemId, C.Text, CS.Name as [Status]
                                from Comment C
                                inner join ContentItem CI on C.PrimaryContentItemId = CI.ContentItemID
                                inner join [SLAM].[List] L on CI.ListID = L.ListID
                                inner join [SLAM].[Site] S on L.SiteID = S.SiteID
                                inner join CommentStatus CS on C.CommentStatusID = CS.CommentStatusID
                                where S.SiteID = @siteID";

            var comments = connection.Query<CommentResource>(query, new { siteID = SiteId });

            return comments;
        }

        public IEnumerable<CommentsReview> GetCommentsCount(Guid SiteId)
        {
            string query = @"   select COUNT(distinct C.CommentID) as Comments, MAX(C.CreatedDate) as LastSubmitted
                                from Comment C
                                inner join ContentItem CI on C.PrimaryContentItemId = CI.ContentItemID
                                inner join [SLAM].[List] L on CI.ListID = L.ListID
                                inner join [SLAM].[Site] S on L.SiteID = S.SiteID
                                where S.SiteID = @siteID";

            var comments = connection.Query<CommentsReview>(query, new { siteID = SiteId});

            return comments;
        }

//        public int UpdateCommentStatus(int commentID)
//        {
//            string query = @"   update [KnowledgeCenterAndInSite].[dbo].[Comment]
//                                set [CommentStatusID] = 's2'
//                                where [CommentID] = @commentId";

//            return connection.Execute(query, new { commentId = commentID});
//        }


	}
}
